//
//  HMEmoticonPageView.swift
//  CZWeibo31
//
//  Created by apple on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// cell 可重用标示符
private let HMEmoticonPageViewCellId = "HMEmoticonPageViewCellId"
class HMEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        // 注册cell
        registerClass(HMEmoticonPageViewCell.self, forCellWithReuseIdentifier: HMEmoticonPageViewCellId)
        // 设置代理
        dataSource = self
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 设置item
        layout.itemSize = self.frame.size
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 滚动方向
        layout.scrollDirection = .Horizontal
    }
}

extension HMEmoticonPageView: UICollectionViewDataSource {
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return HMEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMEmoticonPageViewCellId, forIndexPath: indexPath) as! HMEmoticonPageViewCell
        cell.indexPath = indexPath
        // 赋值
        cell.emoticons = HMEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        cell.backgroundColor = self.backgroundColor
        return cell
    }
}
